package com.tbi.graphcolouring.algorithmconfiguration.mutator;

import java.util.List;

import com.tbi.algorithm.configuration.IMutator;
import com.tbi.graphcolouring.AlgorithmSettings;
import com.tbi.graphcolouring.PopulationEntity;
import com.tbi.graphcolouring.utils.Random;

public class SinglePointMutator implements IMutator{
	private enum MutationDirection{
		INCREMENTIAL, DECREMENTIAL
	}

	@Override
	public void mutation(List<Object> population) {
		float mutationProbability = AlgorithmSettings.MUTATION_PROBABILITY;
		
		for(Object obj : population){
			if(Random.checkSuccess(mutationProbability)){
				PopulationEntity entity = (PopulationEntity) obj;
				if(Random.checkSuccess(0.5f)){
					mutate(entity, MutationDirection.INCREMENTIAL);
				}else{
					mutate(entity, MutationDirection.DECREMENTIAL);
				}
			}
		}
	}
	
	private void mutate(PopulationEntity entity, MutationDirection direction) {
		int mutatingIndex = (int)Random.uniform(0, entity.colors.length);
		
		switch(direction){
		case INCREMENTIAL : 
			entity.colors[mutatingIndex]++;
			if(entity.colors[mutatingIndex] >= entity.colors.length){
				entity.colors[mutatingIndex] = 0;
			}
			break;
		case DECREMENTIAL :  
			entity.colors[mutatingIndex]--;
			if(entity.colors[mutatingIndex] < 0){
				entity.colors[mutatingIndex] =  entity.colors.length - 1;
			}
			break;
		}
	}
}
